package tree;

import com.sun.org.apache.xpath.internal.XPathAPI;

public class MyBinaryTreeNode<T1,T2>{
    private T1 id;
    private T2 element;
    private MyBinaryTreeNode<T1,T2> left;
    private MyBinaryTreeNode<T1,T2> right;

    private boolean leftPre;
    private boolean rightPost;
    public MyBinaryTreeNode() {
    }

    public boolean isLeftPre() {
        return leftPre;
    }

    public void setLeftPre(boolean leftPre) {
        this.leftPre = leftPre;
    }

    public boolean isRightPost() {
        return rightPost;
    }

    public void setRightPost(boolean rightPost) {
        this.rightPost = rightPost;
    }

    public MyBinaryTreeNode(T1 id, T2 element) {
        this.id = id;
        this.element = element;
        this.left = null;
        this.right = null;
    }

    public T1 getId() {
        return id;
    }

    public void setId(T1 id) {
        this.id = id;
    }

    public T2 getElement() {
        return element;
    }

    public void setElement(T2 element) {
        this.element = element;
    }

    public MyBinaryTreeNode<T1,T2> getLeft() {
        return left;
    }

    public void setLeft(MyBinaryTreeNode<T1,T2> left) {
        this.left = left;
    }

    public MyBinaryTreeNode<T1,T2> getRight() {
        return right;
    }

    public void setRight(MyBinaryTreeNode<T1,T2> right) {
        this.right = right;
    }

    public void preOrderPrint(){
        printNode();
        if(left!=null){
            left.preOrderPrint();
        }
        if(right!=null){
            right.preOrderPrint();
        }
    }
    public void printNode(){
        System.out.println("id:"+id+",element:"+element);
    }

    public void add(MyBinaryTreeNode<T1,T2> node,MyBinaryTreeNode<T1,T2> parent) {
        Comparable<T1> comparable = (Comparable<T1>) node.getId();
        if(comparable.compareTo(id)<0){
            if(left==null){
                left = node;
            }else {
                left.add(node,this);
            }
        }else if(comparable.compareTo(id)>0) {
            if(right==null){
                right = node;
            }else {
                right.add(node,this);
            }
        }else {
            node.left = this.left;
            node.right = this.right;
            if(parent!=null){
                if(comparable.compareTo(parent.getId())<0){
                    parent.left = node;
                }else {
                    parent.right = node;
                }
            }
        }
    }

    public T2 search(T1 id) {
        T2 result = null;
        if(this.id.equals(id)){
            result = element;
        }

        if(result==null&&left!=null){
            result = left.search(id);
        }

        if(result==null&&right!=null){
            result = right.search(id);
        }

        return result;
    }

    public void postOrderPrint() {
        if(right!=null){
            right.postOrderPrint();
        }
        if(left!=null){
            left.postOrderPrint();
        }
        printNode();
    }

    public void postOrderThreads(){
        if(right!=null){
            right.postOrderThreads();
        }
        if(left!=null){
            left.postOrderThreads();
        }

    }
}
